package fr.vvins.core.catalog.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import fr.vvins.core.dao.EntityImpl;
import fr.vvins.core.user.entity.UserComment;
import fr.vvins.core.user.entity.UserLog;
import fr.vvins.core.user.entity.UserRating;

/**
 * Product entity
 */
@Entity
@Table(schema="vvins",name="VV_PRODUCT")
@Inheritance(strategy=InheritanceType.JOINED)
public class Product extends EntityImpl implements Serializable {

	private static final long serialVersionUID = 4893994421200748601L;
	@Id
	@GeneratedValue
	@Column(name="id")
	private Integer id;
	
	@Basic
	@Column(name="SHORT_NAME")
	private String shortName;
	
	@Basic
	@Column(name="LONG_NAME")
	private String longName;
	
	@Basic
	@Column(name="DESCRIPTION")
	private String description;
	
	@Basic
	@Column(name="DTYPE")
	private String type;

	@Basic
	@Column(name="SELL")
	private Long sell;

	@Basic
	@Column(name="FAVORITE")
	private Long favorite;
	
	@Basic
	@Column(name="LOG")
	private Long log ;

	@Basic
	@Column(name="SCORE")
	private Long score ;
	
	@ManyToOne
	@JoinColumn(name="CONTENT_ID")
	private Content content;
	
	@Basic
	@Column(name="CREATION_DATE")
	private Date creationDate;
	
	@OneToMany(mappedBy="product")
	private Set<UserComment> userComments = new HashSet<UserComment>(0);
	
	@OneToMany(mappedBy="parentProduct")
	private Set<Sku> skus = new HashSet<Sku>(0);

	@OneToMany(mappedBy="product")
	private Set<ProductRating> productRatings = new HashSet<ProductRating>(0);
	
	@OneToMany(mappedBy="product")
	private Set<UserRating> userRatings = new HashSet<UserRating>(0);
	
	@OneToMany(mappedBy="product")
	private Set<ProductComment> productComments = new HashSet<ProductComment>(0);

	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	@JoinTable(schema="vvins", name="VV_PRODUCT_TAG", joinColumns=@JoinColumn(name="PRODUCT_ID"), inverseJoinColumns=@JoinColumn(name="TAG_ID"))
	private Set<Tag> tags = new HashSet<Tag>(0);

	@OneToMany(mappedBy="product")
	private Set<UserLog> logs = new HashSet<UserLog>(0);
	
	@Transient
	private Long bestPrice;
	
	public Product() {
	}

	public Product(String shortName, String longName, String type,
			Date creationDate, Set<UserComment> userComments,
			Set<Sku> skus) {
		this.shortName = shortName;
		this.longName = longName;
		this.type = type;
		this.creationDate = creationDate;
		this.userComments = userComments;
		this.skus = skus;
	}

	@Override
	public Integer getId() {
		return this.id;
	}

	@Override
	public void setId(Integer id) {
		this.id = id;
	}

	public String getShortName() {
		return this.shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getLongName() {
		return this.longName;
	}

	public void setLongName(String longName) {
		this.longName = longName;
	}

	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Date getCreationDate() {
		return this.creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public Set<UserComment> getUserComments() {
		return userComments;
	}

	public void setUserComments(Set<UserComment> userComments) {
		this.userComments = userComments;
	}

	public Set<Sku> getSkus() {
		return skus;
	}

	public void setSkus(Set<Sku> skus) {
		this.skus = skus;
	}

	public Content getContent() {
		return content;
	}

	public void setContent(Content content) {
		this.content = content;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Set<ProductRating> getProductRatings() {
		return productRatings;
	}

	public void setProductRatings(Set<ProductRating> productRatings) {
		this.productRatings = productRatings;
	}

	public Set<ProductComment> getProductComments() {
		return productComments;
	}

	public void setProductComments(Set<ProductComment> productComments) {
		this.productComments = productComments;
	}

	public Set<Tag> getTags() {
		return tags;
	}

	public void setTags(Set<Tag> tags) {
		this.tags = tags;
	}

	public Long getSell() {
		return sell;
	}

	public void setSell(Long sell) {
		this.sell = sell;
	}

	public Long getFavorite() {
		return favorite;
	}

	public void setFavorite(Long favorite) {
		this.favorite = favorite;
	}
	
	public Long getLog() {
		return log;
	}

	public void setLog(Long log) {
		this.log = log;
	}

	public Set<UserLog> getLogs() {
		return logs;
	}

	public void setLogs(Set<UserLog> logs) {
		this.logs = logs;
	}

	public Long getScore() {
		return score;
	}

	public void setScore(Long score) {
		this.score = score;
	}
	
	public void setBestPrice(Long bestPrice) {
		this.bestPrice = bestPrice;
	}

	public Set<UserRating> getUserRatings() {
		return userRatings;
	}

	public void setUserRatings(Set<UserRating> userRatings) {
		this.userRatings = userRatings;
	}
	
	public Long getBestPrice() {
		if(bestPrice == null) {
			for(Sku s : getSkus()){
				if(bestPrice == null){
					bestPrice = s.getPrice();
				} else if(s.getPrice() < bestPrice){
					bestPrice = s.getPrice();
				}
			}
			
		}
		return bestPrice;
	}	

}
